<?php
namespace admin\action;

use herosphp\bean\Beans;
use herosphp\http\HttpRequest;

/**
 * system_opt_log action
 * @package admin\action
 * @author yangjian<yangjian102621@gmail.com>
 */
class SystemOptLogAction extends CommonAction {

    protected $serviceBean = 'system.systemOptLog.service';
    protected $actionTitle = '操作日志';

    //数据列表页面
    public function index( HttpRequest $request ) {
        $action = $request->getParameter('action', 'trim');
        $method = $request->getParameter('method', 'trim');
        $opt_name = $request->getParameter('opt_name', 'trim');
        $starttime = $request->getParameter('starttime', 'trim');
        $endtime = $request->getParameter('endtime', 'trim');

        $conditions = array();
        if ( $action ) $conditions['action'] = $action;
        if ( $method ) $conditions['method'] = $method;
        if ( $opt_name ) $conditions['opt_name'] = "%{$opt_name}%";
        if ( $starttime ) $conditions['addtime']['>='] = $starttime.' 00:00:00';
        if ( $endtime ) $conditions['addtime']['<='] = $endtime.' 24:00:00';

        $this->setConditions($conditions);
        $this->getAllManagers();
        $this->assign('tableHeader', array('操作人','Action','Method', '操作类别','操作内容','操作时间', '操作IP'));
        if ( $request->getParameter('__export') == 1 ) {

            $this->export($request); //导出数据
        } else {

            parent::index($request);
            $this->setView('log/opt_log');
            $this->assign('breadcrumb', array('系统管理', '操作日志'));
        }
    }

    //导出数据
    protected function export(HttpRequest $request) {
        if( $request->getParameter('__export') == 1 ){
            $ids = $request->getParameter('ids');
            if( $ids ){
                $conditions['id']['$in'] = $ids;
                $this -> setConditions($conditions);
            }
            $this->setPagesize(getConfig('export_max_count'));
            $service = Beans::get($this->getServiceBean());
            $items = $service->where($this->getConditions())->limit(1, $this->getPagesize())->sort(array('addtime' => -1))->find();
            $excel = array();
            foreach($items as $k => $v)
            {
                $tmp = array();
                $tmp[] = $v['opt_name'];
                $tmp[] = $v["action"];
                $tmp[] = $v["method"];
                $tmp[] = $v["opttype"];
                $tmp[] = $v["content"];
                $tmp[] = $v["addtime"];
                $tmp[] = $v["ip"];
                $excel[] = $tmp;
            }
            $header = $this->getTemplateVar('tableHeader');
            phpexcel("管理员操作日志",$header,$excel);
            exit();
        }
    }
}
